package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import school.domain.Category;
import school.domain.Goods;
import school.domain.Notice;
import school.domain.User;
import school.mapper.*;
import school.service.UserService;
import school.support.BaseController;
import school.support.pojo.CommentPo;
import school.support.pojo.GoodsPo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    //首页
    @RequestMapping({"/", "index"})
    public String index(Model model, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "") String name, Integer categoryId) {
        model.addAttribute("errorMsg", session.getAttribute("errorMsg"));
        session.removeAttribute("errorMsg");
        //先把所有分类的东西查出来了
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("g.name like", "%" + name + "%");
        if (categoryId != null && categoryId > 0)
            criteria.andEqualTo("categoryid", categoryId);
        PageHelper.startPage(pageNum, 20);
        PageInfo<GoodsPo> pageInfo = new PageInfo<GoodsPo>(goodsMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        //广告
        List<Notice> noticeList = noticeMapper.selectAll();
        model.addAttribute("noticeList", noticeList);

        List<Category> categoryList = categoryMapper.selectAll();
        session.setAttribute("categoryList", categoryList);

        return "index";
    }

    /**
     * 到注册页面
     *
     * @return
     */
    @GetMapping("register")
    public String register() {
        session.removeAttribute("errorMsg");
        return "register";
    }

    /**
     * 注册用户
     *
     * @return
     */
    @PostMapping("register")
    public String register(User user, Model model) {
        model.addAttribute("errorMsg", session.getAttribute("errorMsg"));
        session.removeAttribute("errorMsg");
        if (userService.selectByUsername(user.getUsername()) != null) {
            model.addAttribute("msg", "用户名已经存在");
            return "index";
        }
        user.setType("user");
        user.setName(user.getUsername());
        user.setAddtime(new Date());
        userMapper.insert(user);

        session.setAttribute("errorMsg", "注册成功，请登录");
        return "redirect:index";
    }


    /**
     * 登录
     *
     * @return
     */
    @PostMapping("login")
    public String login(String phone, String password, HttpSession session, Model model) {
        User user = userService.login(phone, password);
        if (null == user) {
            session.setAttribute("errorMsg", "用户名或密码错误");
            return "redirect:index";
        }
        session.setAttribute("sessionUser", user);
        return "redirect:index";
    }

    /**
     * 退出系统
     *
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("sessionUser");
        return "redirect:index";
    }

    /**
     * 项目详情
     *
     * @param id
     * @return
     */
    @RequestMapping("goods/{id}")
    public String goods(@PathVariable Integer id, Model model) {
        session.removeAttribute("errorMsg");
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        model.addAttribute(goods);

        /**
         * 评论
         */
        List<CommentPo> commentList = commentMapper.selectPoByGoods(id);
        model.addAttribute("commentList", commentList);

        return "detail";
    }

    /**
     * 忘记密码
     *
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("update")
    public String update(User user, Model model) {
        User user1 = userService.selectByUsername(user.getUsername());
        if (null == user1) {
            session.setAttribute("errorMsg", "用户不存在");
            return "redirect:index";
        }
        user1.setPassword(user.getPassword());
        user1.setName(user.getUsername());
        userMapper.updateByPrimaryKeySelective(user1);
        session.setAttribute("errorMsg", "修改成功");
        return "redirect:/index";
    }
}
